/*
 * @Author: your name
 * @Date: 2020-07-20 15:40:49
 * @LastEditTime: 2025-02-25 08:50:11
 * @LastEditors: Lcx l1556461835@163.com
 * @Description: In User Settings Edit
 * @FilePath: \fushun-gitee\plugins\api.js
 */
export default ({ app, store }, inject) => {
  app.$axios.onRequest(config => {
    config.headers.userid = localStorage.getItem("userID") || "";
  });
  inject("postService", async function(url, data, addInfo, isNotify) {
    url = "/api" + url;
    try {
      const result = await app.$axios.$post(url, data, addInfo);
      if (result.code !== 200) {
        this.$message({
          showClose: true,
          message: result.message,
          type: "error"
        });
        return;
      }
      return result;
    } catch (e) {
      if (process.server) return;
      this.$notify.error({
        title: "系统信息",
        message:
          process.env.NODE_ENV === "production"
            ? "通讯出错了，请稍后再试"
            : e.message,
        showClose: true
      });
      return {
        status: "sysError",
        code: e.response && e.response.status ? e.response.status : "401"
      };
    }
  });

  inject("getService", async function(url, data, isNotify) {
    url = "/api" + url;
    try {
      const result = await app.$axios.$get(url, { data });
      return result;
    } catch (e) {
      if (process.server) return;
      this.$notify.error({
        title: "系统信息",
        message:
          process.env.NODE_ENV === "production"
            ? "通讯出错了，请稍后再试"
            : e.message,
        showClose: false
      });
    }
  });
};
